<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 商铺成员管理
 */
class Suser extends Auth_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('suser_mdl');
        $this->load->model('store_mdl');
    }

    /**
     * 查询商铺管理员
     */
    public function index(){
        $store_id = $this->input->get('sid',TRUE);
        $store_id = intval($store_id);

        if(empty($store_id)){
            show_404();
            exit;
        }

        $this->db->where('id',$store_id);
        $res_store = $this->store_mdl->get_one();

        if(empty($res_store['id'])){
            show_error('参数错误！');
            exit;
        }

        $this->db->start_cache();
        $this->db->where('store_id',$store_id);
        $this->db->where('status',1);
        $this->db->stop_cache();

        $this->load->library('pagination');

        $config['base_url'] = site_url('admini/suser/index?sid='.$store_id);
        $config['total_rows'] = $this->suser_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        
        $page = $this->pagination->create_links();

        $per_page = $this->input->get('per_page',TRUE);

        //查询该商铺的用户成员
        $this->db->order_by('permissions asc, id desc');
        $this->db->limit($config['per_page'],intval($per_page));
        $list = $this->suser_mdl->select();
        $this->db->flush_cache();

        $data = [
            'page' => $page,
            'list'=>$list,
            'store'=>$res_store
        ];

        $this->_view('admin/suser_index',$data);
    }

    /**
     * 创建管理员
     */
    public function create(){
        $store_id = $this->input->get('sid',TRUE);
        $store_id = intval($store_id);
        if(empty($store_id)){
            show_error('参数错误！');
            exit;
        }

        $this->db->where('id',$store_id);
        $res_store = $this->store_mdl->get_one();

        if(empty($res_store)){
            show_error('参数错误！');
            exit;
        }

        $data['res_store'] = $res_store;

        $this->_view('admin/suser_create',$data);
    }

    /**
     * 修改用户
     */
    public function update(){
        $sid = $this->input->get('sid',TRUE);
        $sid = intval($sid);
        if(empty($sid)){
            show_404('参数错误！');
            exit;
        }

        $this->db->where('id',$sid);
        $res = $this->suser_mdl->get_one();
        if(empty($res)){
            show_error('参数错误！');
            exit;
        }

        $this->db->where('id',$res['store_id']);
        $res_store = $this->store_mdl->get_one();

        $data = [
            'res' => $res,
            'res_store' => $res_store
        ];
        $this->_view('admin/suser_update',$data);
    }

    /**
     * 用户密码重置
     */
    public function password(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $post = $this->input->post(NULL,TRUE);

        $sid = intval($post['sid']);

        if(empty($sid)){
            die('参数错误！');
        }

        if(!preg_match("/^[0-9a-zA-Z_]{6,20}$/",$post['pass'])){
            die('密码只能为字母、数字和下划线，6-20个字符');
        }

        $this->db->where('id',$sid);
        $res = $this->suser_mdl->update(['password'=>md5($post['pass'])]);
        $this->logs->add($res,'重置商户用户密码');
        echo $res === true ? '1' : '重置密码失败';
    }

    /**
     * 用户删除
     */
    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }
        $sid = $this->input->post('sid',TRUE);
        $sid = intval($sid);

        if(empty($sid)){
            die('参数错误！');
        }

        $this->db->where('id',$sid);
        $res = $this->suser_mdl->delete();
        $this->logs->add($res,'删除用户');
        echo $res === true ? '1' : '删除用户失败！';
    }

    /**
     * 用户操作 添加/修改
     */
    public function operation(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $post = $this->input->post(NULL,TRUE);

        /** 字段验证 */
        if(empty($post['username'])){
            die('用户名不能为空');
        }

        //检查用户名是否重复
        if(empty($post['id'])){

            $this->db->where('username',$post['username']);
            $res = $this->suser_mdl->get_one();
            if(!empty($res)){
                die('用户名重复，请换一个用户名');
            }

            if(!preg_match("/^[0-9a-zA-Z_]{6,20}$/",$post['username'])){
                die('用户名只能为字母、数字和下划线，6-20个字符');
            }
            if($post['password'] != $post['password2']){
                die('两次密码输入不一致！');
            }
            if(!preg_match("/^[0-9a-zA-Z_]{6,20}$/",$post['password'])){
                die('密码只能为字母、数字和下划线，6-20个字符');
            }
            unset($post['password2']);
            $post['password'] = md5($post['password']);

        }

        if(!preg_match('/^[0-9]{8,14}$/',$post['phone'])){
            die('请输入正确的电话号码');
        }

        if(empty($post['name'])){
            die('备注名不能为空');
        }

        if(empty($post['store_id'])){
            die('参数错误！');
        }


        if($post['permissions'] == 1){
            //将当前店铺下所有会员权限改变成 2
            $this->db->where('store_id',$post['store_id']);
            $res = $this->suser_mdl->update(['permissions'=>2]);
            $this->logs->add($res,'设置商铺用户权限');
        }else{
            //判断当前商铺是否有管理员
            $this->db->where('store_id',$post['store_id']);
            $this->db->where('permissions',1);
            $res = $this->suser_mdl->get_one();
            $post['permissions'] = empty($res) ? 1 : 2;
        }

        if(empty($post['id'])){
            $res = $this->suser_mdl->create($post);
            $this->logs->add($res,'添加商铺用户');
        }else{
            $this->db->where('id',intval($post['id']));
            $res = $this->suser_mdl->update($post);
            $this->logs->add($res,'修改商铺用户');
        }

        
        echo $res === true ? '1' : '添加用户失败！';
    }

}

/* End of file Suser.php */
/* Location: ./application/controllers/admini/Suser.php */